#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

void test0()
{
    vector<int> vec = {1, 2, 5, 5, 6, 5, 5, 5, 5, 9};
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //删除vector中所有元素为5的值
    //因为在vector中，元素是连续的，如果删除元素之后，后面的元素
    //会前移，如果再将迭代器进行后移的话，就会漏掉某些元素,导致
    //不能删除连续重复元素
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        if(5 == *it)
        {
            vec.erase(it);
        }
    }

    //删除完毕之后，再遍历
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;
}

void test()
{
    vector<int> vec = {1, 2, 5, 5, 6, 5, 5, 5, 5, 9};
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //删除vector中所有元素为5的值
    for(auto it = vec.begin(); it != vec.end();)
    {
        if(5 == *it)
        {
            /* vec.erase(it++);//error */
            vec.erase(it);//ok
            /* it = vec.erase(it);//ok */
        }
        else
        {
            ++it;
        }
    }

    //删除完毕之后，再遍历
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;
}
int main(int argc, char *argv[])
{
    test();
    return 0;
}

